package com.example.interviewrecorder.view

import android.content.Intent
import android.os.Bundle
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.lifecycleScope
import androidx.lifecycle.repeatOnLifecycle
import com.example.interviewrecorder.LyyApplicaiton
import com.example.interviewrecorder.LyyBaseActivity
import com.example.interviewrecorder.LyyDatabase
import com.example.interviewrecorder.R
import com.example.interviewrecorder.components.showTip
import com.example.interviewrecorder.dao.LyyUserDao
import com.example.interviewrecorder.databinding.ActivityEditPersonalDataBinding
import com.example.interviewrecorder.utils.LyyActivityCollector
import com.example.interviewrecorder.utils.LyyLogUtil
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch

class EditPersonalDataActivity : LyyBaseActivity() {
    private val lyyUserDao: LyyUserDao = LyyDatabase.getDatabase().getLyyUserDao()
    private val binding: ActivityEditPersonalDataBinding by lazy {
        ActivityEditPersonalDataBinding.inflate(layoutInflater)
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(binding.root)
        initEvents()
    }

    private fun initEvents() {
        binding.saveBtn.setOnClickListener {
            if (binding.trueNameRight.text.isNullOrBlank()) {
                showTip(content = getString(R.string.true_name_not_blank_tip))
                return@setOnClickListener
            }
            if (binding.genderRight.text.isNullOrBlank()) {
                showTip(content = getString(R.string.gender_not_blank_tip))
                return@setOnClickListener
            }
            if (binding.ageRight.text.isNullOrBlank()) {
                showTip(content = getString(R.string.age_not_blank_tip))
                return@setOnClickListener
            }
            if (binding.emailRight.text.isNullOrBlank()) {
                showTip(content = getString(R.string.email_not_blank_tip))
                return@setOnClickListener
            }
            lifecycleScope.launch {
                launch(Dispatchers.IO) {
                    repeatOnLifecycle(Lifecycle.State.STARTED) {
                        val currUser = LyyApplicaiton.currUser
                        currUser?.let {
                            runCatching {
                                it.trueName = binding.trueNameRight.text.toString()
                                it.gender = binding.genderRight.text.toString()
                                it.age = binding.ageRight.text.toString().toInt()
                                it.email = binding.emailRight.text.toString()
                                lyyUserDao.updateLyyUser(it)
                            }.onSuccess {
                                runOnUiThread {
                                    showTip(
                                        content = getString(R.string.update_success),
                                        onPositiveBtnClick = {
                                            finish()
                                        }
                                    )
                                }
                            }.onFailure {
                                LyyLogUtil.e(TAG, it.toString())
                                runOnUiThread {
                                    showTip(
                                        content = getString(R.string.update_fail_tip),
                                    )
                                }
                            }
                        }
                    }
                }
            }
        }
    }

    companion object {
        private const val TAG = "EditPersonalDataActivit"

        fun start() {
            val currAct = LyyActivityCollector.getCurrActivity()
            val intent = Intent(currAct, EditPersonalDataActivity::class.java)
            currAct.startActivity(intent)
        }
    }

    override fun onResume() {
        super.onResume()
        initData()
    }

    private fun initData() {
        val currUser = LyyApplicaiton.currUser
        currUser?.let {
            binding.userNameRight.text = it.userName
            binding.trueNameRight.setText(it.trueName)
            binding.genderRight.setText(it.gender)
            binding.ageRight.setText("${it.age}")
            binding.emailRight.setText(it.email)
        }
    }
}